package ca.ets.osgi.hotelservice;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;

/**
 * Contain a list of Hotels
 * @author HP_Administrator
 *
 */
public class ListHotels implements Iterable<HotelAbs>{
	
	//
	private Hashtable<String,HotelAbs> hTable = new Hashtable<String,HotelAbs>();
	
	//
	private static ListHotels instance;
	
	//
	private ListHotels(){}
	
	public static ListHotels getInstance(){
		if(instance ==null)
			instance = new ListHotels();
		return instance;
	}

	@Override
	public Iterator<HotelAbs> iterator() {
		// TODO Auto-generated method stub
		return this.iterator();
	}
	
	/**
	 * Add an hotel to the list
	 * @param city
	 * @param habs
	 */
	public void addHotelToList(String hotelID, HotelAbs habs){
		this.hTable.put(hotelID, habs);
	}
	
	/**
	 * Remove an hotel from the list
	 * @param hotelID
	 * @param city
	 */
	public void removeHotelFromList(String hotelID, String city){
		Enumeration<HotelAbs> emu = this.hTable.elements();
		while(emu.hasMoreElements()){
			HotelInfo hotel = (HotelInfo) emu.nextElement();
			if(hotel.getAdresse().getVille().equalsIgnoreCase(city)==true && hotel.getHotelID().equalsIgnoreCase(hotelID)==true){
				this.hTable.remove(hotelID);
			}
		}
		
	}
	
	/**
	 * Retrieve an hotel from the list
	 * @param hotelID
	 * @param city
	 * @return
	 */
	public HotelAbs getHotel(String hotelID, String city){
		HotelInfo hotel2= null;
		Enumeration<HotelAbs> emu = this.hTable.elements();
		while(emu.hasMoreElements()){
			HotelInfo hotel = (HotelInfo) emu.nextElement();
			if(hotel.getAdresse().getVille().equalsIgnoreCase(city)==true && hotel.getHotelID().equalsIgnoreCase(hotelID)==true){
				hotel2 = hotel;
			}
		}
		return hotel2;
	}
	
	/**
	 * Return a list of hotels by city
	 * @param city
	 * @return
	 */
	public ArrayList<HotelAbs> getHotelsFromCity(String city){
		ArrayList<HotelAbs> list = new ArrayList<HotelAbs>();
		Enumeration<HotelAbs> emu = this.hTable.elements();
		while(emu.hasMoreElements()){
			HotelInfo hotel  = (HotelInfo) emu.nextElement();
			if(hotel.getAdresse().getVille().equalsIgnoreCase(city)==true){
				list.add(hotel);
			}
		}
		
		if(list.size()==0){
			list = null;
		}
		return list;
	}
	
	
	/**
	 * Return a list of hotels by city
	 * @param city
	 * @return
	 */
	public ArrayList<HotelAbs> getHotelsByCityAndAvailability(String city){
		ArrayList<HotelAbs> list = new ArrayList<HotelAbs>();
		Enumeration<HotelAbs> emu = this.hTable.elements();
		while(emu.hasMoreElements()){
			HotelInfo hotel  = (HotelInfo) emu.nextElement();
			if(hotel.getAdresse().getVille().equalsIgnoreCase(city)==true){
				if(hotel.isAvailability())
					list.add(hotel);
			}
		}
		if(list.size()==0){
			list = null;
		}
		return list;
	}

}
